using System;
using System.Collections;
using System.Collections.Generic;
using Awaiter;
using Nsb.Classes;
using YesPlayLove.Classes;
using YesPlayLove.Clusters;
using jQueryApi;

namespace YoshiFlowMain
{
    //[IgnoreNamespace] Don't use because full class name is used in Master.cs to find this class
    public sealed class FeaturesPage : YoshiPage
    {
        private static readonly Dictionary<string, string> FeaturesDb = new Dictionary("PartitionKey", "db/meta/features", "RowKey", "overview", "Class", "Features");
        private jQueryObject _outer;

        public FeaturesPage()
        {
            new Await().AddAw(BasicBuildAw, GamesTheme.PageTitle).Commit();
        }

        public override void RefreshAw(Await awp)
        {
            _outer = jQuery.FromHtml("<div/>").AppendTo(PageCluster).AddClass(FeaturesDb["Class"]);

            new Await()
                .AddAw(EditListHtml.ShowHtmlListAw, FeaturesDb, _outer)
                .AddDx(
                    delegate
                    {
                        // Add the edit events
                        jQuery.Select("h2,li", _outer)
                            .Click(
                                delegate(jQueryEvent e)
                                {
                                    CancelEvent(e);
                                    new Await()
                                        .AddAw(EditListHtml.Aw, jQuery.FromElement(e.Target))
                                        .HandleDl(Exceptions.UserCancelled, Await.FinishAw)
                                        .AddDl(delegate(Await aw) { if ((bool)aw["changed"]) FileBlobBase.PutBlobAw(aw, EditListHtml.ScoopData(_outer), FeaturesDb); })
                                        .HandleDl(Exceptions.FileIsNewer, delegate(Await aw) { new Ask().OkAw(aw, ThemeBase.IsCheckedOut); })
                                        .Commit();
                                }
                            )
                            ;

                        // Add section buttons
                        jQuery.Select(".SectionJar", _outer)
                            .Prepend(DuplicateBtn(new Dictionary("top", "3px", "left", "-26px"), DupeSectAw))
                            .Append(DeleteBtn(new Dictionary("top", "3px", "right", "-20px")))
                            ;

                        // Add item buttons
                        jQuery.Select(".ItemJar", _outer)
                            .Prepend(DuplicateBtn(new Dictionary("top", "0px", "left", "-86px"), DupeItemAw))
                            .Append(DeleteBtn(new Dictionary("top", "0px", "right", "-40px")))
                            ;
                    }
                )
                .Commit(awp);
        }

        public void DeleteAw(Await awp, jQueryObject elToDel)
        {
            elToDel.Remove();
            awp.Done();
        }

        public jQueryObject DeleteBtn(Dictionary options)
        {
            Dictionary css = new Dictionary();
            css["position"] = Script.Value((string)options["position"], "absolute");
            css["top"] = Script.Value((string)options["top"], "0px");
            css["right"] = Script.Value((string)options["right"], "-20px");
            if (options.ContainsKey("css"))
                jQuery.ExtendObject((object)css, options["css"]);
            return jQuery.FromHtml("<span class='SimpleDeleteBtn'/>")
                .CSS(css)
                .Attribute("title", ThemeBase.DeleteBtnTitle)
                .Html("-")
                .Click(
                    delegate(jQueryEvent e)
                    {
                        CancelEvent(e);
                        new Await()
                            .AddAw(new Ask().OkCancelAw, ThemeBase.ForSure)
                            .AddAw(RequireResultAw, true)
                            .AddAw(DeleteAw, jQuery.FromElement(e.Target).Closest(".Jar"))
                            .HandleDl(Exceptions.UserCancelled, Await.AbortAw)
                            .AddDl(delegate(Await aw) { FileBlobBase.PutBlobAw(aw, EditListHtml.ScoopData(_outer), FeaturesDb); })
                            .Commit();
                    }
                )
                ;
        }

        public jQueryObject DuplicateBtn(Dictionary options, Action<Await,jQueryObject> action)
        {
            Dictionary css = new Dictionary();
            css["position"] = Script.Value((string) options["position"], "absolute");
            css["top"] = Script.Value((string) options["top"], "0px");
            css["left"] = Script.Value((string) options["left"], "-20px");
            if (options.ContainsKey("css"))
                jQuery.ExtendObject((object)css, options["css"]);
            return jQuery.FromHtml("<span class='SimpleAddLineBtn'/>")
                .CSS(css)
                .Html("+")
                .Attribute("title", ThemeBase.AddNewItemBtnTitle)
                .Click(
                    delegate(jQueryEvent e)
                    {
                        CancelEvent(e);
                        new Await()
                            .AddAw(action, jQuery.FromElement(e.Target).Closest(".Jar"))
                            .HandleDl(Exceptions.UserCancelled, Await.AbortAw)
                            .AddDl(delegate(Await aw) { FileBlobBase.PutBlobAw(aw, EditListHtml.ScoopData(_outer), FeaturesDb); })
                            .Commit();
                    }
                );
        }

        public void DupeItemAw(Await awp, jQueryObject el)
        {
            el.Clone(true, true)
                .InsertAfter(el)
                .Find("li")
                .Html("")
                .Click()
                ;
            awp.Done();
        }

        public void DupeSectAw(Await awp, jQueryObject el)
        {
            jQueryObject sect = el.Clone(true, true)
                .InsertAfter(el)
                ;

            sect
                .Find(".ItemJar")
                .Slice(1)
                .Remove()
                ;

            sect
                .Find("li")
                .Html("")
                ;

            sect
                .Find("h2")
                .Html("New topic")
                .Click()
                ;

            awp.Done();
        }
    }
}